<?php

namespace app\user\logic;

use app\model\HealthFitness;
use app\model\Medicine;

class HealthFitnessLogic
{
    protected HealthFitness $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new HealthFitness();
        $this->factory_id = request()->user_info['factory_id'];
    }

    /**
     * @Description 列表
     * @author: zj
     * @param array $param
     * @time 2024/03/25 16:42:04
     * @return mixed
     */
    public function getList(array $param)
    {
        $query = $this->model->alias('hf')
            ->join('pig p','hf.out_pig_id = p.out_id','left')
            ->join('fence fe','fe.out_id = p.out_fence_id','left')
            ->join('barn b','b.out_id = fe.out_barn_id','left')
            ->join('medicine m','m.out_id = hf.out_dietary_id','left')
            ->field('hf.out_id as id,hf.out_pig_id as pig_id,p.tag_address,p.out_fence_id as fence_id,fe.name as fence,fe.out_barn_id as barn_id,b.name as barn,hf.out_dietary_id as medicine_id,m.name as medicine,hf.weight,hf.remark,hf.record_time,hf.create_time')
            ->where('hf.factory_id',$this->factory_id)
            ->where('hf.delete_time',$this->model::IS_DELETE_NO);
        if ($param['type']) {
            $query->where('m.type',$param['type']);
        }
        if ($param['barn_id']) {
            $query->where('fe.out_barn_id',$param['barn_id']);
        }
        if ($param['fence_id']) {
            $query->where('p.out_fence_id',$param['fence_id']);
        }
        if ($param['medicine_id']) {
            $query->where('hf.out_dietary_id',$param['medicine_id']);
        }
        if ($param['record_time_start']) {
            $query->where('hf.record_time','>=',strtotime($param['record_time_start']));
        }
        if ($param['record_time_end']) {
            $query->where('hf.record_time','<=',strtotime($param['record_time_end']));
        }

        return $query->order('hf.out_id DESC')
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

    /**
     * @Description 统计
     * @author: zj
     * @param array $param
     * @time 2024/03/28 15:10:35
     * @return array
     */
    public function statistic(array $param)
    {
        $medicineModel = new Medicine();
        $medicine = $medicineModel->field('out_id,name')
            ->where('factory_id',$this->factory_id)
            ->where('type',$param['type'])
            ->where('delete_time',$medicineModel::IS_DELETE_NO)
            ->distinct(true)
            ->select()
            ->toArray();
        if (!$medicine) {
            return [];
        }

        $data = [];
        foreach ($medicine as $value) {
            $query = $this->model->alias('hf')
                ->join('pig p','hf.out_pig_id = p.out_id','left')
                ->join('fence fe','fe.out_id = p.out_fence_id','left')
                ->where('hf.factory_id',$this->factory_id)
                ->where('hf.out_dietary_id',$value['out_id'])
                ->where('hf.delete_time',$this->model::IS_DELETE_NO);

            if ($param['barn_id']) {
                $query->where('fe.out_barn_id',$param['barn_id']);
            }
            if ($param['fence_id']) {
                $query->where('p.out_fence_id',$param['fence_id']);
            }
            if ($param['record_time_start']) {
                $query->where('record_time','>=',strtotime($param['record_time_start']));
            }
            if ($param['record_time_end']) {
                $query->where('record_time','<=',strtotime($param['record_time_end']));
            }
            $weight = $query->sum('hf.weight')?:0;

            $data[] = [
                'medicine' => $value['name'],
                'weight' => $weight
            ];
        }

        return $data;
    }
}